<template>
  <el-dialog v-bind="{ ...$attrs }" class="app-add">
    <el-tabs type="card">
      <el-tab-pane label="网址/电话/Email">
        <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="120px" status-icon>
          <el-form-item label="网域" prop="web">
            <el-select v-model="ruleForm.domain">
              <el-option v-for="(item, index) in domainList" :label="item.name" :value="item.name"
                :key="index"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="自定义hash" prop="hash">
            <el-radio-group v-model="radio1" class="ml-4">
              <el-radio label="1" size="large">不需要</el-radio>
              <el-radio label="2" size="large">需要</el-radio>
            </el-radio-group>
            <el-input v-model="ruleForm.hash" v-if="radio1 == '2'" placeholder="请输入后缀" maxlength="8"></el-input>
          </el-form-item>

          <el-form-item label="模式" prop="web">
            <el-select v-model="ruleForm.redirectMode">
              <el-option label="轮询模式" :value="0"></el-option>
              <el-option label="记忆模式" :value="1"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-tab-pane>
    </el-tabs>
    <template #footer>
      <el-button @click="$emit('close')"> 关闭 </el-button>
      <el-button type="primary" @click="submitForm(ruleFormRef)"> 确认 </el-button>
    </template>
  </el-dialog>
</template>
 
<script lang="ts" setup>
import { reactive, ref, onBeforeMount } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { setLine } from '@/server/home'
import { getDomainList } from '@/server/domain'
import { ElMessage, ElMessageBox } from 'element-plus'
const emit = defineEmits<{ (event: 'close'): void; (event: 'success'): void; }>()
//自定义属性
const props: any = defineProps({
  lineItem: {}
})
//定义属性
interface RuleForm {
  hash: string,
  domain: string,
  redirectMode: number
}
const radio1 = ref(props.lineItem.hash ? "2" : "1")
//表单
const formSize = ref('large')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive({
  hash: props.lineItem.hash,
  domain: props.lineItem.domain,
  redirectMode: props.lineItem.redirectMode
})
const validatePass = (rule: any, value: any, callback: any) => {
  if (radio1.value == '2') {
    if (value === '') {
      callback(new Error('hash不能为空'))
    } else if (/[^\w_]/.test(ruleForm.hash)) {
      callback(new Error("只能输入数字或字母"))
    }
  }
  callback()
}
const rules = reactive<FormRules<RuleForm>>({
  hash: [{ validator: validatePass, trigger: 'blur' }],
})
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      try {
        await setLine(props.lineItem.id, ruleForm)
        ElMessage.success("修改成功")
        emit("success")
      } catch (err) {
        ElMessage.error("修改失败")
      }

    } else {
      console.log('error submit!', fields)
    }
  })
}

//获取网址列表
const domainList = ref()
const getDomainListData = async () => {
  const params = {
    SkipCount: 0,
    MaxResultCount: 1000
  }
  const data = await getDomainList(params)
  domainList.value = data.items
}
onBeforeMount(() => {
  getDomainListData()
})
</script>
<style lang="scss" scoped>
.el-dialog {
  height: 500px !important;
  overflow: auto;
  // .el-tabs{
  //   height: 400px;
  //   overflow: auto;
  // }
}

.btn {
  display: flex;
  align-items: flex-end;
  width: 100%;

  .el-input {
    flex: 1;
  }

  .el-button {
    margin-left: 0;
  }
}

.app-add {
  .el-tag {
    margin-top: 10px;
    margin-right: 10px;
  }
}
</style>